package tpf.designer.pattern.course5.structure_pattern2.flyweight_13.simple_2;

import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 享元工厂
 */
public class FlyweightFactory {

    public static final Logger LOGGER = Logger.getLogger(FlyweightFactory.class);

    /**
     * 缓存多个flyweight对象，这里只是示意一下
     */
    private Map<String,Flyweight> fsMap = new HashMap<String,Flyweight>();
    /**
     * 获取key对应的享元对象
     * @param key 获取享元对象的key，只是示意
     * @return key 对应的享元对象
     */
    public Flyweight getFlyweight(String key) {
        //这个方法里面基本的实现步骤如下：
        //1：先从缓存里面查找，是否存在key对应的Flyweight对象
        Flyweight f = fsMap.get(key);
        //2：如果存在，就返回相对应的Flyweight对象
        if(f==null){
            //3：如果不存在
            //3.1：创建一个新的Flyweight对象
            f = new ConcreteFlyweight(key);
            //3.2：把这个新的Flyweight对象添加到缓存里面
            fsMap.put(key,f);
            //3.3：然后返回这个新的Flyweight对象
        }
        return f;
    }

    /**
     * 辅助方法
     */
    public void checkFlyweight(){
        Flyweight fly;
        int i = 0;
        for (Map.Entry<String, Flyweight> stringFlyweightEntry : fsMap.entrySet()) {
            Map.Entry e = stringFlyweightEntry;
            LOGGER.info("享元对象" + (++i) + ":" + e.getKey());
        }
        /*for (Iterator it = fsMap.entrySet().iterator(); it.hasNext(); ){
            Map.Entry e = (Map.Entry)it.next();
            LOGGER.info("享元对象" + (++i) + ":" + e.getKey());
        }*/
    }
}